package pers.qianyu.module.system.controller;


import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import pers.qianyu.module.core.comm.*;
import pers.qianyu.module.core.domain.system.dto.*;
import pers.qianyu.module.core.domain.system.query.*;
import pers.qianyu.module.core.domain.system.vo.*;
import pers.qianyu.module.system.service.*;

import java.util.*;

/**
 * @author mizzle rain
 * @date 2020-11-13 13:10
 */
@Api(tags = "用户-角色关联接口")
@RestController
@RequestMapping("/system-serv")
public class SysUserRoleController {
    private final SysUserRoleService sysUserRoleService;

    public SysUserRoleController(SysUserRoleService sysUserRoleService) {
        this.sysUserRoleService = sysUserRoleService;
    }

    @ApiOperation("根据用户id查询对应角色")
    @GetMapping("/user_role/user/{id}")
    public HttpResult<List<SysRoleVO>> queryByUserId(@PathVariable("id") Long userId) {
        List<SysRoleVO> sysRoleVOS = sysUserRoleService.queryByUserId(userId);
        return HttpResult.okWithData(sysRoleVOS);
    }

    @ApiOperation("根据角色id查询所有具有该角色的用户")
    @GetMapping("/user_role/role")
    public HttpResult<Pagination<SysUserVO>> queryByRoleId(SysUserRoleQuery sysUserRoleQuery) {
        Pagination<SysUserVO> roleVOList = sysUserRoleService.queryByRoleId(sysUserRoleQuery);
        return HttpResult.okWithData(roleVOList);
    }

    @ApiOperation("给用户赋权")
    @PostMapping("/user_role")
    public HttpResult<?> insert(@RequestBody SysUserRoleDTO sysUserRoleDTO) {
        sysUserRoleService.insUserRole(sysUserRoleDTO);
        return HttpResult.okWithMessage("用户赋权成功");
    }

    @ApiOperation("删除用户权限")
    @DeleteMapping("/user_role")
    public HttpResult<?> delete(@RequestBody SysUserRoleDTO sysUserRoleDTO) {
        sysUserRoleService.delAuthUserRole(sysUserRoleDTO);
        return HttpResult.okWithMessage("删除用户权限成功");
    }
}
